 function getRootPath()
 {
 	 var strFullPath=window.document.location.href;
	 var strPath=window.document.location.pathname;
	 var pos=strFullPath.indexOf(strPath);
	 var prePath=strFullPath.substring(0,pos);
	 var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
	 return postPath;
 }
 var rootPath=getRootPath();
 
Ext.onReady(function(){
     var clickleafid=-1;	 
      var moudeltreeLoder=new Ext.tree.TreeLoader({
             dataUrl:rootPath+'/sendisystem/listmoudletree.do'
        });
    var 	moudeltreeroot=new Ext.tree.AsyncTreeNode({
         text:'根模块',
		 id:-1
         });
         
 
   var  moudletreePanel=new Ext.tree.TreePanel({
      	id: 'tree-panel',
    	title: '模块信息',
        renderTo:"treediv",
         bodyStyle: 'padding-bottom:15px;background:#eee;',
        split: true,
        height: 500,
        minSize: 150,
        autoScroll: true,
        rootVisible:true,
        lines: true,
        singleExpand: false,
        useArrows: true,
        loader:moudeltreeLoder,
        root: moudeltreeroot
         }); 
         
     
    moudletreePanel.on("beforeload", function(node) {
          moudeltreeLoder.baseParams.Treetext = node.text;
          moudeltreeLoder.baseParams.Treeid=node.id;
    }); 
    
    
   moudletreePanel.on("click",function(Node,checked ){
   	     clickleafid=Node.id;
   	     store.reload({params:{start:0, limit:25,clickleafid:Node.id,reloadflag:'yes'}})
   },moudletreePanel);
   
   //////////
   
   	var sm=new Ext.grid.CheckboxSelectionModel({
	singleSelect:false
 	});   
	var colml=new Ext.grid.ColumnModel([
	        new Ext.grid.RowNumberer(),
	        sm,
		    {id:'id',header: "ID", width: 40, sortable: true, dataIndex: 'id',hidden :true},
            {header: "模块标题", width: 20, sortable: true,  dataIndex: 'moudletitle'},
            {header: "模块自身的ID", width: 20, sortable: true, dataIndex: 'moudleid'},
            {header: "模块链接", width: 20, sortable: true, dataIndex: 'openurl'},
            {header: "图片路径", width: 20, sortable: true, dataIndex: 'icon'},
            {header: "图片样式", width: 20, sortable: true, dataIndex: 'iconcls'},
            {header: "排序", width: 20, sortable: true, dataIndex: 'orderid'}
         
            ]
    );
    var store=new Ext.data.JsonStore({
    	root: 'data',
    	totalProperty: 'totalCount',
    	pruneModifiedRecords:true,//设置为true,则每次当store装载或有record被移除时,清空所有修改了的record信息. 默认为false. 
  		fields: [ 'id','moudletitle','moudleid','openurl','icon','orderid','iconcls','addshutcut','shutcuturl'],
		url: rootPath+'/sendisystem/moudleInit.do' ,
		remoteSort:true
    });
     var pagebar=new Ext.PagingToolbar({
        pageSize:10,
        store:store,
        displayInfo:true,
        displayMsg:'显示第 {0}  条 到 {1} 条记录，一共{2}条 ',
        emptyMsg:"没有记录"
        });
    
        
        	   var addform={
	   	            usepurse:'addform', //用途,默认情况下是添加，也可以是modifyform
                    id:'addform',
                    xtype:'form',
                    url:rootPath+'/sendisystem/addMoudlefunction.do',
                   layout:"column",
                   baseCls:"x-plain",
                   style:"padding:5px",
                   items:[{
                      columnWidth:.5,
                      layout:"form",
                      labelWidth:70,
                      baseCls:"x-plain",
                      items:[{
		                    xtype:'textfield',
		                    name:'moudletitle',fieldLabel: '模块名', id:'moudletitle',
		                    allowBlank:false,
		                    anchor:'95%'
		                }, {
		                     xtype:'textfield',
		                     name: 'openurl',fieldLabel: '模块URL',id:'openurl',
		                     anchor:'95%'
		                },{
		                    xtype:'textfield',
		                    name: 'icon',fieldLabel: '图片路径',id:'icon',
		                    anchor:'95%'
		                },{
		                 xtype:'textfield',
		                 name:'iconcls',fieldLabel:'图片样式名',id:'iconcls',  anchor:'95%'
		                },{
		                    xtype:'textfield',
		                    name: 'shutcuturl',fieldLabel: '桌面图标',id:'shutcuturl',
		                    anchor:'95%'
		                }]
                   },{
                     columnWidth:.5,
                     layout:"form",
                      labelWidth:90,
                     baseCls:"x-plain",
		             items:[
		             	  {
		                    xtype:'textfield',
		                    name: 'moudleid',fieldLabel: '模块ID',  allowBlank:false,
		                    anchor:'95%'
		                },{
		                       xtype:'numberfield',
		                       name: 'orderid',fieldLabel: '排序', id:'orderid', allowBlank:false,
		                      anchor:'95%'
		                }
		                ,{
		                    xtype:'checkbox',
		                    name: 'isleaf',fieldLabel: '功能模块',
		                    anchor:'95%'
		                }
		                 ,{
		                    xtype:'checkbox',
		                    name: 'addshutcut',fieldLabel: '桌面快捷方式',
		                    anchor:'95%'
		                },
		                {
		                    xtype:'hidden',
		                    name: 'id'
		                 },
		                 {
		                    xtype:'hidden',
		                    name: 'parentid',
		                    id:'parentid'
		                 }
		                ]
                   }]
                
                };
	   
	   //////// add window
	   
	 var addwin=new Ext.Window({
                     title:"增加/修改模块信息",
				     bodyStyle:'padding:5px',
				     layout : 'fit',
				     closeAction :'hide',
				 	 plain: true,
			         frame: true,
			         id:'addwind',
			         width: 500,
				     height:250,
				     layout:"form",
                     labelWidth:55,
                    items:[addform],
                showlock:false,
                buttons:[
                {
                 text:"确定",
                handler:function()
                {
                	  var basicform=Ext.getCmp('addform').getForm();
                  if( basicform.isValid())
                  {
                  	   basicform.findField('parentid').setValue(clickleafid);
                  	   basicform.submit({
                  						 
		                 		  		   waitTitle:"等待中.....",
		                 		 		   waitMsg:"正在提交数据，请稍.....",
		                 		 		   failure:function()
		                 		 		   {
		                 		 		   		  Ext.Msg.alert('信息', ' 操作失败，请检查服务器!');   
		                 		 		   		    addwin.hide();
		                 		 		   },
		                 		 		   success:function(_form,_action)
		                 		 		   {
		                 		 		   	    Ext.Msg.alert('信息', '操作成功!');
		                 		 		   	    addwin.hide();
		                 		 		   	    store.load({params:{start:0, limit:25,clickleafid:clickleafid,reloadflag:'yes'}});
		                 		 		       moudeltreeroot.reload();
		                 		 	 	   }
		                 	 	 		 });
                  }
               
                }
                },
                	
                {
                 text:"取消",
                 handler:function()
                 {
                  addwin.hide();
                 }
                
                }
                
                ]
            });
            
            
   var actionbar=new Ext.Toolbar({
      scope:this,height:25, items:[
					            {
					              xtype:'button',
					              text:'增加',
					              	 handler:function()
									 {
									 	
									    addwin.show();
									    var basicform=Ext.getCmp('addform').getForm();
									    basicform.reset();
									    basicform.url=rootPath+'/sendisystem/addMoudlefunction.do';
									 }
					            	
					            },'-',
					               {
					               		 xtype:'button',
							 	        text:'修改',
										 handler:function()
										 {
										 		var _sm=datagrid.selModel;
												if(_sm.getCount()==0)
												{
												    Ext.Msg.alert('信息','请选择需要修改记录，注意只能单选');
												    return;
												}
												if(sm.getCount()>1)
												{
												   Ext.Msg.alert('信息','每次只能修改一条记录,现取第一条选择的记录做修改');
												}
												 
												var selec_record=_sm.getSelected();
												var id=selec_record.get("id");
												Ext.Ajax.request({
											    url:rootPath+'/sendisystem/excutModifyQuerymoudle.do',
											    success:function(_response,_options){
											   	var obj=Ext.util.JSON.decode(_response.responseText);  //
											   	
											     var basicform=Ext.getCmp('addform').getForm();
											       addwin.show();
											   	   basicform.setValues(obj.datas);  //注意设置form值的语句需要在show函数后，因为此时的form还没有创建or Render
											   	  
											   	   basicform.url=rootPath+'/sendisystem/excuteModifymoudle.do'
											   	 
							 					},
											   failure: function()
											   {
											    Ext.Msg.alert('信息','查询失败');
											    },
											   params: {id: id }
											});
							  				 
										 }
							 	    },'-',
									   {
							            text: '删除',
							            handler:function()
							            {
							                    var _sm=datagrid.selModel;
												if(_sm.getCount()==0)
												{
												    Ext.Msg.alert('信息','请选择需要删除记录');
												    return;
												}
											    var selec_record=_sm.getSelections();
											    var len=selec_record.length;
											    var id="'";
											    for(var leni=0;leni<len;leni++)
											    {
											    	
											    	if(leni==len-1)
											    	{
											    		id+=selec_record[leni].get("id")+"'";
											    	}
											    	else
											    	{
											    	 id+=selec_record[leni].get("id")+"','";
											    	}
											    }
										 		Ext.Ajax.request({
											    url:rootPath+'/sendisystem/excuteDeletemoudle.do',
											    success:function(_response,_options){
											        Ext.Msg.alert('信息','删除成功');
											           store.load({params:{start:0, limit:25,clickleafid:clickleafid,reloadflag:'yes'}});
											         moudeltreeroot.reload();
							 					},
											   failure: function()
											   {
											    Ext.Msg.alert('信息','查询失败');
											   },
											   params: {deleterecord: id }
											});
												
												
							            }
							        }
					            ]
					            
   })     
    var datagrid=new Ext.grid.GridPanel({
    	renderTo:"grid",
       height:500,
        title:'模块管理',
        store: store,
        cm:colml,
     	sm:sm,
      
        trackMouseOver:false,
        disableSelection:true,
        loadMask: {msg:'正加载数据...'},
        viewConfig: {
            forceFit:true            
        },
        bbar:pagebar,
       tbar:actionbar
     });
    
 
 
  
})